package com.withoutwords.engine.settings;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;

import com.withoutwords.engine.Game;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.MyInfo;
import com.withoutwords.engine.social.SocialUser;

public class Settings {

	final static String pushTokenKey = "pushTokenKey";
	final static String currentGameIdKey = "currentGameIdKey";
	final static String soundVolumeKey = "soundVolumeKey";
	SharedPreferences mPrefs;
	MyInfo me;
	String pushToken = "";
	int soundVolume = -1;

	public int getSoundVolume() {
		if (soundVolume == -1) {
			if (mPrefs != null)
				soundVolume = mPrefs.getInt(soundVolumeKey, 50);
		}
		return soundVolume;
	}

	public void setSoundVolume(int soundVolume) {
		if (this.soundVolume != soundVolume) {
			this.soundVolume = soundVolume;
			if (mPrefs != null) {

				SharedPreferences.Editor editor = mPrefs.edit();
				editor.putInt(soundVolumeKey, soundVolume);
				editor.commit();
			}
		}
	}

	public String getPushToken() {
		if (pushToken.equalsIgnoreCase("")) {
			if (mPrefs != null)
				pushToken = mPrefs.getString(pushTokenKey, "");
		}
		return pushToken;
	}

	public void setPushToken(String pushtoken) {
		if (!pushToken.equals(pushtoken)) {
			this.pushToken = pushtoken;
			if (mPrefs != null) {

				SharedPreferences.Editor editor = mPrefs.edit();
				editor.putString(pushTokenKey, pushToken);
				editor.commit();
			}
		}
	}

	public String getCurrentGameId() {
		String res = "";
		if (mPrefs != null)
			res = mPrefs.getString(currentGameIdKey, "");
		return res;

	}

	public void setCurrentGameId(String gameId) {

		if (mPrefs != null) {

			SharedPreferences.Editor editor = mPrefs.edit();
			editor.putString(currentGameIdKey, gameId);
			editor.commit();
		}

	}

	private Settings() {

	}

	static Settings mSettingsInstance;

	public static Settings getInstance() {
		if (mSettingsInstance == null)
			mSettingsInstance = new Settings();
		return mSettingsInstance;
	}

	public void setPrefrences(SharedPreferences prefs) {
		if(mPrefs ==  null && prefs != null)
		mPrefs = prefs;
	}

	public String getCurrentUserId() {
		try {
			return getMyInfo().getUserId();
		} catch (NullPointerException e) {
			return "";
		}
	}

	public void setMyInfo(String myId, SocialUser mySocialInfo, String myEmail,
			boolean bSaveToPrefrences) {
		me = new MyInfo();
		me.setEmail(myEmail);
		me.setSocialUser(mySocialInfo);
		me.setUserId(myId);
		setMyInfo(me, bSaveToPrefrences);
	}

	public MyInfo getMyInfo() {
		if (me == null) {

			if (mPrefs != null) {
				String meJsonString = mPrefs.getString("me", "");
				try {
					JSONObject meJsonObj = new JSONObject(meJsonString);
					me = new MyInfo();
					me.fromJSON(meJsonObj);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return me;
	}

	public void setMyInfo(MyInfo me, boolean bSaveToPrefrences) {
		this.me = me;
		if (bSaveToPrefrences) {
			if (mPrefs != null) {
				SharedPreferences.Editor editor = mPrefs.edit();
				editor.putString("me", me.toJSON().toString());

				editor.commit();
			}
		}

	}
	public void saveOpenedGames(ArrayList<Game> OpenedGames )
	{
		JSONArray arr = new JSONArray();
		for (Game g : OpenedGames) {
			arr.put(g.toJSON());
			
		}
		String jsonGames = arr.toString();
		if (mPrefs != null) {
			SharedPreferences.Editor editor = mPrefs.edit();
			editor.putString("OpenedGames", jsonGames);

			editor.commit();
		}
	}
	public ArrayList<Game> loadOpenedGames()
	{
		ArrayList<Game> games = new ArrayList<Game>();
		if (mPrefs != null) {
			String JsonString = mPrefs.getString("OpenedGames", "");
			try {
				JSONArray arr = new JSONArray(JsonString);
				for(int i =0 ; i< arr.length() ; i++)
				{
					JSONObject jsonObj  = arr.getJSONObject(i);
					Game g = new Game();
					g.fromJSON(jsonObj);
				games.add(g );
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	}
		return games;
	}

}
